package com.icode.web.controller.manager;

import com.icode.core.model.manage.Investigate;
import com.icode.core.shared.LuckHandle;
import com.icode.core.shared.ValidateNumHandle;
import com.icode.service.user.LuckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-3-18
 * Time: 上午11:29
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/luck")
public class LuckController {

    @Autowired
    private LuckService luckService;
    private int first = 0;
    private int second = 0;
    private int third = 0;
    private int partake = 0;

    @RequestMapping(value = "toLuck", method = RequestMethod.GET)
    public String show() {
        List<Investigate> investigates = luckService.findAllInvestigate();
        LuckHandle.initInvestigates(investigates);
        return "front/manager/luck";
    }

    @RequestMapping(value = "start/{prize}", method = RequestMethod.GET)
    @ResponseBody
    public String startLuck(@PathVariable String prize) {
        if ("一等奖".equals(prize) && first < 1) {
            first += 1;
        } else if ("二等奖".equals(prize) && second < 3) {
            second += 1;
        } else if ("三等奖".equals(prize) && third < 5) {
            third += 1;
        } else if ("参与奖".equals(prize) && partake < 10) {
            partake += 1;
        } else {
            return "fail";
        }
        String result = LuckHandle.selectLuckPerson();
        return result;
    }

    @RequestMapping(value = "sendMsg/{content}", method = RequestMethod.GET)
    @ResponseBody
    public String sendLuckMessage(@PathVariable String content) {
        String result = "success";
        try {
            String[] array = content.split("\\&");
            for (int i = 0; i < array.length; i++) {
                String data[] = array[i].split("\\^");
                ValidateNumHandle.sendLuckMsg(data[0], data[1], data[2]);
            }
        } catch (Exception e) {
            result = "error";
        }
        return result;
    }

}
